package com.sluggard.security.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sluggard.common.model.res.Result;
import com.sluggard.mp.mvc.BaseController;
import com.sluggard.security.manage.model.entity.UserEntity;
import com.sluggard.security.manage.model.entity.UserRoleEntity;
import com.sluggard.security.manage.model.req.ChangeStatusReq;
import com.sluggard.security.manage.model.req.UserReq;
import com.sluggard.security.manage.service.IUserRoleService;
import com.sluggard.security.manage.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户管理 前端控制器
 * </p>
 *
 * @author lizheng
 * @since 2024-04-28
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/manage/system/user")
public class UserController extends BaseController<UserReq, UserEntity, IUserService> {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private IUserRoleService userRoleService;

    @Operation(summary = "修改密码")
    @PostMapping("changePassword")
    public Result<Void> changePassword(@RequestBody @Valid UserReq req) {
        service.changePassword(req);
        return Result.ok("修改成功");
    }

    @Override
    protected void afterSave(UserReq req, UserEntity instance) {
        // 解绑原关系
        userRoleService.deleteByUserId(instance.getId());
        // 绑定用户角色关系
        if (!CollectionUtils.isEmpty(req.getRoleIds())) {
            Set<UserRoleEntity> collect = req.getRoleIds().stream()
                    .map(item -> {
                        UserRoleEntity userRoleEntity = new UserRoleEntity();
                        userRoleEntity.setUserId(instance.getId());
                        userRoleEntity.setRoleId(item);
                        return userRoleEntity;
                    }).collect(Collectors.toSet());
            userRoleService.saveOrUpdateBatch(collect);
        }
    }

    @Override
    protected UserEntity loadEntity(Long id) {
        return service.loadEntity(id);
    }

    @Override
    protected void onTables(QueryWrapper<UserEntity> chainWrapper, Page<UserEntity> page) {
        service.onTables(chainWrapper, page);
    }

    @Override
    protected List<UserEntity> onList(QueryWrapper<UserEntity> chainWrapper) {
        return service.onList(chainWrapper);
    }

    @Override
    protected void beforeSave(UserReq req, UserEntity instance) {
        super.beforeSave(req, instance);
        if (StringUtils.isNoneBlank(req.getPassword())) {
            req.setPassword(passwordEncoder.encode(req.getPassword()));
        }
    }

    @Operation(summary = "启用/禁用")
    @PostMapping("changeStatus")
    public Result<Void> changeStatus(@Validated @RequestBody ChangeStatusReq req) {
        service.changeStatus(req);
        return Result.ok();
    }
}
